package com.ymatou.iapi.productquery.testcase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productquery.compare.TConsummer;
import com.ymatou.iapi.productquery.parameter.RecommendProd.RecommendProductBean;
import com.ymatou.iapi.productquery.service.GetSellerRecommendProductListCall;
import com.ymatou.iapi.sellerproduct.service.SetRecommendCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AssertService;
import com.ymttest.business.service.LiveCallService;
import com.ymttest.business.service.ProductPriceCallService;
import com.ymttest.business.service.ProductQueryCallService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;

public class Ts_GetSellerRecommendProductList {
	private static GetSellerRecommendProductListCall getSellerRecommendProductListCall = new GetSellerRecommendProductListCall();
	private static Integer sellerId = Integer.valueOf(EnvSetup.getData("productpriceSellerId"));
	private static String sellerName = EnvSetup.getData("productpriceSellerName");	
	
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("买手推荐的商品列表");
	}

	@Before
	public void caseUp() {
		
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getSellerRecommendProductListCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	

	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSellerRecommendProductList_001() {
		Logger.start(true,"买手推荐的商品列表");
		try{
			String productId1 = ProductPriceCallService.getProdPriceWithActivity_NoNewBuyerAndNoIsolation(sellerId).getProductId();
			
			String productId2 = ProductPriceCallService.getProdPriceWithActivity_NoNewBuyerAndNoIsolation(sellerId).getProductId();
			
			//发布商品加入直播
			JSONObject liveBean = LiveCallService.createLiveInProgress(sellerName,sellerId);
			int liveId = liveBean.getJSONObject("Data").getInt("ActivityId");
			String productId3 =	SellerProductCallService.addNewProductNotOnSale(sellerId,sellerName,null).getJSONObject("Data").getString("ProductID");
			SellerProductCallService.addProductsInLive(sellerId,liveId,Arrays.asList(productId3));

			SetRecommendCall.setrecommendcall(MapUtil.hashMap("UserId", sellerId, "ActivityId", liveId, "ProductId", productId3));		
			
			
			checkProdBySellerId(sellerId,MapUtil.hashMap(productId1, false, productId2, false,productId3,true));
			
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static void checkProdBySellerId(int sellerId,Map<String,Boolean> prodIdAndisTopMap) throws Exception{
		List<String> prodList = new ArrayList<>();
		List<RecommendProductBean> tgtRecommendProductBeanList = new ArrayList<>();
		prodIdAndisTopMap.forEach((x,y)->{
			prodList.add(x);
			RecommendProductBean bean = new RecommendProductBean();
			bean.setProductId(x);
			bean.setIsTopProduct(y);
			tgtRecommendProductBeanList.add(bean);
		});
		
		
		
		JSONObject ret = getSellerRecommendProductListCall.search(Arrays.asList(((Integer)sellerId).toString()));
		String chkdesc = "GetSellerRecommendProductList";
		Logger.verifyEquals(200, ret.getInt("Code"), "返回码检查");
		
		List<RecommendProductBean> actRecommendProductBeanList = new ArrayList<>();
		JSONArray prodListAry = ret.getJSONObject("Data").getJSONArray("ProductList");
		for(int i=0;i<prodListAry.length();i++){
			if (prodList.contains(prodListAry.getJSONObject(i).getString("ProductId"))){
				actRecommendProductBeanList.add(ProductQueryCallService.getBeanFromJson(prodListAry.getJSONObject(i),RecommendProductBean.class));
			}
		}
		
		ProductQueryCallService.compareListBean(prodList, tgtRecommendProductBeanList, actRecommendProductBeanList, chkdesc, 
										(x)->x.getProductId(), new TConsummer(){
											public void accept(Object arg0, Object arg1, Object arg2) {
												Map<String, Object> tgtMap = MapUtil.pojoToMap((RecommendProductBean)arg0);
												Map<String, Object> actMap = MapUtil.pojoToMap((RecommendProductBean)arg1);
												AssertService.assertResultEqual(tgtMap,actMap,String.valueOf(arg2));
											}
											
										});
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
